package de.placidi.pressreview.beans;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.primefaces.model.tagcloud.DefaultTagCloudItem;
import org.primefaces.model.tagcloud.DefaultTagCloudModel;
import org.primefaces.model.tagcloud.TagCloudModel;

import de.placidi.pressreview.parsing.CountedWord;
import de.placidi.pressreview.textprocessing.StatisticWords;
import de.placidi.pressreview.textprocessing.TextProcessor;
import de.placidi.pressreview.textprocessing.TextprocessorImpl;

@ManagedBean
@SessionScoped
public class ReviewBean {
	
	private StatisticWords stats;
	private TagCloudModel model;
	private TextProcessor textprocessor;
	
	public ReviewBean() {	
		model = new DefaultTagCloudModel();	
	}
	
	public void doScan() {
		
		URL url = ReviewBean.class.getResource("/de/placidi/pressreview/resource/headlines.txt");
		File f = new File(url.getFile());
		
		textprocessor = new TextprocessorImpl("http://localhost:8080/PressReview/resources/english-bidirectional-distsim.tagger", f);
		
		textprocessor.addExpressionToCut("bbc");
		textprocessor.addExpressionToCut("news");
		textprocessor.addExpressionToCut("\\s[s]\\s");	
		textprocessor.addExpressionToExclude("homepage");
		textprocessor.addExpressionToExclude("profile");
		textprocessor.addExpressionToExclude("overview");
		
		try {
			stats = textprocessor.getStatistics();
		} catch (IOException e) {
			e.printStackTrace();
		}
			
		feedCloudModel();

	}

	private void feedCloudModel() {
		
		List<CountedWord> topWords = stats.getTopWords(10, 5);
		model = new DefaultTagCloudModel();
		for (CountedWord topWord : topWords) {
				
			String label = topWord.getWord()+ " " + stats.getAdjMap().get(topWord.getWord()).getTopThree();
			Integer strength = topWord.getFrequency();
			System.out.println(label +" " +strength);
			
			model.addTag(new DefaultTagCloudItem(label, strength));
		}
	}
	

	public TagCloudModel getModel() {
		return model;
	}
	
	
}
